<template>
  <div>
    <el-table
      :data="tableData.slice((currentPage - 1) * 5, currentPage * 5)"
      border
      style="width: 100%"
    >
      <el-table-column prop="id" label="用户id" :show-overflow-tooltip="true">
      </el-table-column>
      <el-table-column prop="userName" label="用户名"> </el-table-column>
      <el-table-column label="用户头像">
        <template slot-scope="scope">
          <el-popover placement="top-start" title="" trigger="hover">
            <img
              :src="scope.row.avatar"
              alt=""
              style="width: 150px; height: 150px"
            />
            <img
              slot="reference"
              :src="scope.row.avatar"
              style="width: 60px; height: 60px"
            />
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column
        prop="email"
        label="用户邮箱"
        :show-overflow-tooltip="true"
      ></el-table-column>
      <el-table-column prop="power_text" label="用户权限"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            type="danger"
            size="mini"
            icon="el-icon-delete"
            @click="del(scope.row)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      layout=" total, prev, pager, next"
      :page-size="5"
      :total="tableData.length"
      @current-change="handleCurrentChange"
    >
    </el-pagination>
  </div>
</template>

<script>
import { getFunc, postFunc } from "@/utils/http.js";

export default {
  data() {
    return {
      tableData: [
        {
          card_id: "2016-05-02",
          name: "王小虎",
          description: "上海市普陀区金沙江路 1518 弄",
          price: "55",
          img: "http://127.0.0.1:1018/static/upload/avatar.png",
          payer_num: "555",
        },
      ],
      currentPage: 1,
    };
  },
  computed: {
    power() {
      return this.$store.state.user.power;
    },
  },
  methods: {
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`);
      this.currentPage = val;
    },
    del(row) {
      console.log(row);
      if (this.power) {
        this.$confirm("此操作将永久删除该用户, 是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(() => {
            let { id } = row;
            if (!row.power) {
              postFunc("/user/del", {
                id,
              }).then((data) => {
                if (data.code == 200) {
                  // this.tableData = data.result;
                  this.$message({
                    type: "success",
                    message: "删除成功!",
                  });
                  this.getData();
                } else {
                  this.$message({
                    type: "error",
                    message: "删除失败!",
                  });
                }
              });
            } else {
              this.$message({
                type: "info",
                message: "您权限不足",
              });
            }
          })
          .catch(() => {
            this.$message({
              type: "info",
              message: "已取消",
            });
          });
      } else {
        this.$message({
          type: "info",
          message: "您权限不足",
        });
      }
    },
    async getData() {
      // 请求商品列表
      await getFunc("/user/list").then((data) => {
        if (data.code == 200) {
          this.tableData = data.res;
          this.tableData.forEach((item) => {
            item.power
              ? (item.power_text = "管理员")
              : (item.power_text = "普通用户");
          });
        }
      });
    },
  },
  mounted() {
    this.getData();
  },
};
</script>

<style lang='scss' scoped>
</style>
